/********************************************************************** 
 * FILE		: BaseUtil.java					
 *			
 * AUTHOR   : kloyf		
 * 
 * DATE		: 2011-6-17
 * 									
 * REASON	: CREATE  
 *            							
 * VERSION  ：0.0.1
 *
 ***********************************************************************/
package com.common.util;

import org.apache.log4j.Logger;

import java.util.regex.Pattern;

/**
 * 工具类的基类。<br>
 * 所有的工具类继承BaseUtil
 * 
 * @author kloyf
 * @version 0.0.1
 */
public class BaseUtil {

	private static final Logger logger = null;

	private static Logger getLogger(String classString) {
		return Logger.getLogger(classString);
	}

	private static Logger getLogger(Class clazz) {
		return Logger.getLogger(clazz);
	}

	public static void error(Class clazz, String msg) {
		getLogger(clazz).error(clazz.getName() + "====" + msg);
	}

	public static void error(Class clazz, Exception ex) {
		getLogger(clazz).error(clazz.getName(), ex.fillInStackTrace());
	}

	public static void debug(Class clazz, String msg) {
		getLogger(clazz).debug(clazz.getName() + "====" + msg);
	}

	public static void debug(Class clazz, Exception ex) {
		getLogger(clazz).debug(clazz.getName(), ex.fillInStackTrace());
	}

	public static void info(Class clazz, String msg) {
		getLogger(clazz).info(clazz.getName() + "====" + msg);
	}

	public static void info(Class clazz, Exception ex) {
		getLogger(clazz).info(clazz.getName(), ex.fillInStackTrace());
	}

	public static void fatal(Class clazz, String msg) {
		getLogger(clazz).fatal(clazz.getName() + "====" + msg);
	}

	public static void fatal(Class clazz, Exception ex) {
		getLogger(clazz).fatal(clazz.getName(), ex.fillInStackTrace());
	}

	/**
	 * 功能描述：对象是否为空
	 * 
	 * @param object
	 *            Object 需要判断的对象
	 * @return boolean 判断对象是否为空。当对象为空时返回true，为假返回false
	 */
	public static boolean isEmpty(Object object) {
		if (null == object) {
			return true;
		}
		return false;
	}

	/**
	 * 功能描述：对象转换为字符串
	 * 
	 * @param object
	 *            Object 需要转换的对象
	 * @return String 转换结果
	 */
	public static String toString(Object object) {
		if (isEmpty(object)) {
			return "";
		}
		return object.toString();
	}

	/**
	 * 功能描述：根据正则表达式进行验证
	 * 
	 * @param object
	 *            Object 传入的对象
	 * @param regex
	 *            String 传入的正则表达式
	 * @return boolean 为真返回true,否则返回false
	 */
	public static boolean relace(Object object, String regex) {
		Pattern pattern = Pattern.compile(regex);
		return pattern.matcher(toString(object)).matches();
	}
}
